﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Model;

namespace TrainingsApp.Logic.Import {
    public abstract class ImportBase : IDataImport {


        static readonly Dictionary<string, long> Provinces;

        static readonly Dictionary<string, long> Areas;

        static readonly Dictionary<string, long> Regions;

        static readonly Dictionary<string, int> Towns;

        const int ALL_COUNT = 10000;

        static ImportBase() {
            //Initialize DB

            Provinces = TS_Province_Logic.TS_Province_ListByPaging(ALL_COUNT, 1).TS_Province_List.ToDictionary(u => u.ProvinceName, u => u.Idx.Value);
            Areas = TS_Area_Logic.TS_Area_ListByPaging(ALL_COUNT, 1).TS_Area_List.ToDictionary(u => u.AreaName, u => u.Idx.Value);
            Regions = TS_Region_Logic.TS_Region_ListByPaging(ALL_COUNT, 1).TS_Region_List.ToDictionary(u => string.Format("{0}-{1}", u.AreaId, u.RegionName), u => u.Idx.Value);
            Towns = TS_Town_Logic.TS_Town_ListByPaging(ALL_COUNT, 1).TS_Town_List.ToDictionary(u => string.Format("{0}-{1}", u.RegionId, u.TownName), u => u.Idx.Value);
        }


        public abstract bool Import(int keyId, string location, ImportType type);


        protected long GetProvinceId(string provinceName) {
            long idx = 0;
            if (Provinces.TryGetValue(provinceName, out idx)) {
                return idx;
            }
            else {
                var entity = TS_Province_Logic.TS_Province_Add(new TS_Province_Entity {
                    ProvinceName = provinceName,
                    Sort = 99
                });
                Provinces.Add(entity.ProvinceName, entity.Idx.Value);
                return entity.Idx.Value;
            }

        }


        protected long GetAreaId(string areaName, long provinceId) {
            long idx = 0;
            if (Areas.TryGetValue(areaName, out idx)) {
                return idx;
            }
            else {
                var entity = TS_Area_Logic.TS_Area_Add(new TS_Area_Entity {
                    AreaName = areaName,
                    Sort = 99,
                    ProvinceId = provinceId
                });
                Areas.Add(entity.AreaName, entity.Idx.Value);
                return entity.Idx.Value;
            }
        }


        protected long GetRegionId(string regionName, long areaId) {

            long idx = 0;
            if (Regions.TryGetValue(string.Format("{0}-{1}", areaId, regionName), out idx)) {
                return idx;
            }
            else {
                var entity = TS_Region_Logic.TS_Region_Add(new TS_Region_Entity {
                    RegionName = regionName,
                    Sort = 99,
                    AreaId = areaId
                });
                Regions.Add(string.Format("{0}-{1}", areaId, regionName), entity.Idx.Value);
                return entity.Idx.Value;
            }
        }


        protected long GetTownId(string townName, long regionId) {

            int idx = 0;
            if (Towns.TryGetValue(string.Format("{0}-{1}", regionId, townName), out idx)) {
                return idx;
            }
            else {
                var entity = TS_Town_Logic.TS_Town_Add(new TS_Town_Entity {
                    TownName = townName,
                    Sort = 99,
                    RegionId = (int)regionId
                });
                Towns.Add(string.Format("{0}-{1}", regionId, townName), entity.Idx.Value);
                return entity.Idx.Value;
            }
        }
    }
}
